package com.mingqijia.gassafety.job.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mingqijia.gassafety.db.entity.Contacts;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.entity.RunningStrategy;
import com.mingqijia.gassafety.db.entity.RunningStrategyExt;
import com.mingqijia.gassafety.db.mapper.ContactsMapper;
import com.mingqijia.gassafety.job.dto.SendMessageDTO;
import com.mingqijia.gassafety.job.service.impl.RunningStrategyServiceImpl;
import com.mingqijia.gassafety.job.service.impl.ServicePeriodStrategyServiceImpl;
import com.mingqijia.gassafety.job.strategy.MessageStrategy;
import com.mingqijia.gassafety.job.strategy.MessageStrategy;
import com.mingqijia.gassafety.job.strategy.RecordService;
import com.mingqijia.gassafety.shared.constant.MessagePushTypeEnum;
import com.mingqijia.gassafety.shared.utils.commonUtils.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 推送短信消息 策略实现
 * </p>
 *
 * @author mby
 * @since 2024-01-16
 */
@Service
@Slf4j
public class SendSmsMessageStrategy implements MessageStrategy { // 实现策略

    @Autowired
    ServicePeriodStrategyServiceImpl servicePeriodStrategyService;

    /**
     * 发送消息
     * @param data 发送数据
     * @param job job名称
     * @param recordService 通知记录保存服务
     */
    @Override
    public void sendMessage(List<SendMessageDTO> data, String job, RecordService recordService) {
        if (CollectionUtils.isNotEmpty(data)) {
            data.stream()
                .filter(x -> x.getPushTypeCode().equals(MessagePushTypeEnum.SMS.getValue()))
                .forEach(x -> {
                    log.info("{}-sendMessage-sms:imei:{},phones:{}", job, x.getImei(), x.getPhones());
                    if (CollectionUtils.isNotEmpty(x.getPhones())) {
                        x.getPhones().stream().forEach(phone -> {
                            x.setPhone(phone.getTelephone());
                            x.setLevel(phone.getLevel());
                            x.setNotifyObjType(phone.getNotifyObjType());
                            try {
                                servicePeriodStrategyService.pushSMS(x, job, recordService);
                            } catch (Exception e) {
                                log.info("{}-sendMessage-sms-error:{}", job, e);
                            }
                        });
                    }
            });
        }

    }
}
